<?php
/**
  *@function 得到所有的分类信息
  *
  *@date 2013-05-29 20:17
  *
  */
class M_order extends CI_Model {
	
	private $table_name = 'zy_order';
	
    function __construct()
    {
        parent::__construct();
		$this->load->database();
    }
    
    /**
     * 将用户的订单加入数据库
     * 
     * @param array 用户订单信息
     * 
     * @return boolean
     */
    public function insertOrder($data)
    {
    	$data = array(
    			'order_user_id' =>$this->session->userdata('user_id')?$this->session->userdata('user_id'):0,//用户id
    			'order_username'=>$data['user_name'],//用户昵称
    			'order_user_phone'=>$data['phoneNumber'],//手机号码
    			'order_province'=>16,//省
    			'order_city'=>220,//市
    			'order_area'=>1834,//区
    			'order_address'=>$data['address'],//用户详细地址
    			'order_number'=>$data['order_number'],//订单号
    			'order_create_time'=>time(),//订单创建时间
    			'order_user_remark'=>$data['user_remark'],
    			'order_type'=>'2'
    	);
    	if($this->db->insert( $this->table_name , $data ))
    	{
    		return $this->db->insert_id();
    	}
    }
    
	/**
     * 将用户的订单加入数据库
     * 
     * @param array 用户订单信息
     * 
     * @return boolean
     */
    public function insertOrderDetail($data)
    {
    	$data = array(
    			'order_id'=>$data['order_id'],//用户昵称
    			'order_price'=>$data['price'],//实际购买价格，（单价）
    			'order_quantity'=>$data['book_quantity'],//购买商品数量
    			'order_total_price'=>$data['price']*$data['book_quantity'],//这一单总价格，数量*单价
    			'book_id'=>$data['book_id']
    	);
    	if($this->db->insert( 'zy_order_details' , $data ))
    	{
    		return true;
    	}
    }
    
    /**
     * 更新用户订单总价格，是通过所有订单详情计算出来后填入
     * 
     * @param int $order_id 订单的ID
     * @param float $orderTotalPrice 需要修改的订单总价格
     * 
     * @return boolean
     */
    public function updateOrderTotalPrice($order_id , $orderTotalPrice)
    {
    	$data = array('order_total_price'=>$orderTotalPrice);
    	$this->db->where('order_id' , $order_id);
    	if($this->db->update( $this->table_name , $data))
    	{
    		return true;
    	}
    }
    
    /**
     * 检索出用户的订单
     * 
     * @param int $user_id 用户的ID
     * 
     * @return array 用户订单组成的数组
     */
    public function selectUserOrderById($user_id,$page,$pagesize)
    {
    	$query = "SELECT * 
    	FROM `{$this->table_name}`
    	WHERE `order_user_id`='".$user_id."' 
    	AND `order_del`='1' 
    	ORDER BY `order_id` DESC LIMIT ".$page.",".$pagesize;
    	return $this->db->query($query)->result();
    }
    
    /**
     * 获取用户订单总量
     */
    public function selectUserOrderAmount($user_id)
    {
    	$query = "SELECT COUNT(*) AS orderAmount FROM `{$this->table_name}` WHERE `order_user_id`='".$user_id."' AND `order_del`='1'";
    	return $this->db->query($query)->result();
    }
    
    /**
     * 获取二级订单详情
     */
    public function selectOrderDetailsImg($order_id)
    {
    	$query = "SELECT b.`book_img`,b.`book_id` 
    	FROM `zy_order_details` as od,`zy_books` as b
    	WHERE od.`book_id`=b.`book_id`
    	AND  od.`order_id`='".$order_id."'
    	ORDER BY `order_details_id` DESC";
    	return $this->db->query($query)->result();
    }
    
    /**
     * 更改订单状态
     */
    /*public function changeOrderStatus($status,$id)
    {
    	$data = array('status'=>$status);
    	$this->db->where('id',$id);
    	if($this->db->update('fz_order',$data))
    	{
    		return true;
    	}
    }*/
    
    /**
     * 用户更改订单状态
     */
    public function changeUserOrderStatus($status,$id)
    {
    	$data = array('order_status'=>$status);
    	$this->db->where('order_id',$id);
    	$this->db->where('order_user_id',$this->session->userdata('user_id'));
    	if($this->db->update($this->table_name,$data))
    	{
    		return true;
    	}
    }
    
    /*public function orderRemark($remark,$id)
    {
    	$data = array('admin_remark'=>$remark);
    	$this->db->where('id',$id);
    	if($this->db->update('fz_order',$data))
    	{
    		return true;
    	}
    }*/
    
    /**
     * 逻辑删除订单
     */
    /*public function hideOrder($order_id)
    {
    	$this->db->where('id',$order_id);
    	if($this->db->update("fz_order",array('is_del'=>'0','user_id'=>$this->session->userdata('user_id'))))
    	{
    		return true;
    	}
    }*/
    
    /**
     * 检索单条订单
     */
    public function selectOrderById($order_id)
    {
    	return $this->db->get_where( $this->table_name , array('order_id'=>$order_id))->result();
    }

    
    

    
    

}